/*
 * Dictionary.h
 *
 *  Created on: 2017年6月20日
 *      Author: heqian
 */

#ifndef MMSEG_DICTIONARY_H_
#define MMSEG_DICTIONARY_H_

#include <list>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "CharNode.h"
#include <functional>

using namespace std;

namespace mmseg {

typedef function<void(string)> OutWord;

class DicFile {
public:
	DicFile(const string& path);
	~DicFile();

	void addFile(const string& path);
	DicFile* getNext();

	const string& getFile() const;

protected:
	string mFile;
	DicFile* mNext;
};

class Dictionary {
public:
	Dictionary();
	virtual ~Dictionary();

	static Dictionary& getInstance();
	void freeDictionary();

	void addWord(const string& str, EnumPos pos, const string& other);

	CharNode* getCharNode();
	bool isNumber(UTF_8 key);
	bool isLower(UTF_8 key);
	bool isUpper(UTF_8 key);
	bool isLowerSymbol(UTF_8 key);
	bool isUpperSymbol(UTF_8 key);
	void init(DicFile* files);
	DicFile* defaultDictionary();
	static string getExtDirectory();
	void save(const char* savePath);
	void outWord(OutWord out);

private:
	list<DicFile> mDicFiles;
	CharNode* mNode;
	DicFile* mDefaultDic;
	UTF_8* muSymobl;

	static Dictionary* mInstance;
	UTF_8 getChar(const char*, int* len);
	void readDicFile(CharNode** node, const string& path);
	void addWord(CharNode** node, const char* str, int num, EnumPos pos, const string& other);
	bool isUtf_8(FILE* file);
	void addDicWord(CharNode** node, char* str, int len);
	void outWord(CharNode* node, const char* str, OutWord out, bool isNext);
	const string outKey(UTF_8 key);

};

} /* namespace mmseg */

#endif /* MMSEG_DICTIONARY_H_ */
